package common_classes;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class HeaderWorker {
	
	String method;
	String filePath;
	String httpVersion;
	String adminName;
	String adminPassword;
	String adminComand;
	int cSID;
	long cTime;
	boolean willCreateCookie;
	
	public HeaderWorker() {
		this.method = "";
		this.filePath = "";
		this.httpVersion = "";
		this.adminName = "";
		this.adminPassword = "";
		this.adminComand = "";
		this.cSID = -1;
		this.cTime = 0;
		this.willCreateCookie = false;
	}
	
	public HeaderWorker(Socket socket) throws HeaderWorkerException {
		this();
		readSocket(socket);
	}
	
	private void readSocket(Socket socket) throws HeaderWorkerException {
		if (socket == null) return;
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String text = "";
			while (true) {
				String s = br.readLine();
				if ((s == null) || s.equals("")) break;
				text += s + "\r\n";
			}

			if (text.contains("Cookie: ")) 
				this.getCookieData(text);
			
			if (text.contains("HTTP\\1.1")) {
				this.httpVersion = "HTTP\\1.1";
			}
			if (text.contains("HTTP\\1.0"))
				this.httpVersion = "HTTP\\1.0";
			
			
			if (text.startsWith("GET")) {
				this.method = "GET";
				this.getFilePath(text);
			} 
			
			if (text.startsWith("POST")) {
				this.method = "POST";
			}
			
			if (text.startsWith("SERVER")) {
				this.method = "SERVER";
				this.getServerData(text);
			} 
		} catch (IOException e) {
			System.err.println(e);
			throw new HeaderWorkerException("Can't parse header");
		}
	}
	
	private void getCookieData(String text) throws HeaderWorkerException {
		String []list = text.split("\r\n");
		String s = "";
		for (int i = 0; i < list.length; i++)
			if (list[i].startsWith("Cookie: ")) {
				s = list[i];
				break;
			}
		s = s.substring(s.indexOf("sid"), s.length());
		list = s.split("; ");
		if (list.length >= 2) {
			this.cSID = Integer.parseInt(list[0].substring(list[0].indexOf("=") + 1, list[0].indexOf(",")));
			this.cTime = Long.parseLong(list[1].substring(list[1].indexOf("=") + 1, list[1].indexOf(",")));
		} else throw new HeaderWorkerException("Can't read cookie");
	}
	
	private void getServerData(String text) throws HeaderWorkerException {
		String []list = text.split("\r\n");
		if (list.length != 4)
			throw new HeaderWorkerException("Can't read header");
		this.adminName = list[1];
		this.adminPassword = list[2];
		this.adminComand = list[3];
	}
	
	private void getFilePath(final String text) throws HeaderWorkerException {
		String str = (text.split("\r\n"))[0];
		if (!str.contains("GET ")) 
			throw new HeaderWorkerException("Can't read header");
		str = str.substring(4);
		if (!str.contains(" HTTP")) 
			throw new HeaderWorkerException("Can't read header");
		str = str.substring(0, str.indexOf(" HTTP"));
		if (str.contains("/../")) 
			throw new HeaderWorkerException("Can't read header");
		this.filePath = str;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String file) {
		this.filePath = file;
	}

	public String getHttpVersion() {
		return httpVersion;
	}

	public void setHttpVersion(String httpVersion) {
		this.httpVersion = httpVersion;
	}
	
	public String getAdminName() {
		return this.adminName;
	}
	
	public String getAdminPassword() {
		return this.adminPassword;
	}
	
	public String getAdminComand() {
		return this.adminComand;
	}


	public boolean isWillCreateCookie() {
		return willCreateCookie;
	}

	public void setWillCreateCookie(boolean willCreateCookie) {
		this.willCreateCookie = willCreateCookie;
	}

	public int getcSID() {
		return cSID;
	}

	public void setcSID(int cSID) {
		this.cSID = cSID;
	}

	public long getcTime() {
		return cTime;
	}

	public void setcTime(long cTime) {
		this.cTime = cTime;
	}
}
